<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

class myUser extends sfBasicSecurityUser {
	
//	public function isAuthenticated() {
//		if ($this->isFacebookUser()) {
//			try {
//				FacebookUtility::get_info(709259, 'first_name');
//			} catch (FacebookRestClientException $ex) {
//				return false;
//			}
//		}
//		return parent::isAuthenticated();
//	}
	
	public function login($user_id) {
		$this->setAttribute('id', $user_id);
		$this->setAuthenticated(true);

		// retrieve the user
		$user = UserPeer::retrieveById($this->getId());
		$username = $user->getUsername();
		$this->setAttribute('username', $username);
		
		$network_id = $user->getNetworkId();
		$this->setAttribute('network_id', $network_id);
		
		$is_admin = $user->getAdmin();
		if($is_admin) {
			$this->setAttribute('admin', true);			
		} else {
			$this->setAttribute('admin', false);
		}
		$this->setAttribute('facebook_id', $user->getFacebookId());
		$this->setAttribute('full_name', $user->getFullName());
		
		// update when the user last logged in
		$user->setLastLoginAt(time());
		$user->save();
	}
	
	public function getId() {
		return $this->getAttribute('id');
	}
	
	public function getUsername() {
		return $this->getAttribute('username');
	}
	
	public function getFullName() {
		return $this->getAttribute('full_name');
	}
	
	public function getNetworkDisplayName() {
		$network_id = $this->getNetworkId();
		return UserPeer::$networks[$network_id];
	}
	
	public function getNetworkId() {
		return $this->getAttribute('network_id');
	}
	
	public function isAdmin() {
		return $this->getAttribute('admin', false);
	}
	
	public function isGroupAdmin($group_id) {
		$role = GroupMemberPeer::NONE;
		$namespace = 'group_member_'.$group_id;
		// check the user's session if the user's role has already been retrieved once
		if($this->hasAttribute('role', $namespace)) {
			$role = $this->getAttribute('role', GroupMemberPeer::NONE, $namespace);
		} else {
			$role = $this->getGroupRole($group_id);
			// cache and store the role in the user's session
			$this->setAttribute('role', $role, $namespace);
		}
		
		// determine the role
		if($role == GroupMemberPeer::ADMIN) {
			return true;
		} else {
			return $this->getAttribute('admin', false);
		}
	}
	
	public function isGroupContributor($group_id) {
		$role = GroupMemberPeer::NONE;
		$namespace = 'group_member_'.$group_id;
		// check the user's session if the user's role has already been retrieved once
		if($this->hasAttribute('role', $namespace)) {
			$role = $this->getAttribute('role', GroupMemberPeer::NONE, $namespace);
		} else {
			$role = $this->getGroupRole($group_id);
			// cache and store the role in the user's session
			$this->setAttribute('role', $role, $namespace);
		}
		
		// determine the role
		if($role == GroupMemberPeer::CONTRIBUTOR) {
			return true;
		} else {
			return false;
		}
	}
	public function isGroupViewer($group_id) {
		$role = GroupMemberPeer::NONE;
		$namespace = 'group_member_'.$group_id;
		// check the user's session if the user's role has already been retrieved once
		if($this->hasAttribute('role', $namespace)) {
			$role = $this->getAttribute('role', GroupMemberPeer::NONE, $namespace);
		} else {
			$role = $this->getGroupRole($group_id);
			// cache and store the role in the user's session
			$this->setAttribute('role', $role, $namespace);
		}
		
		// determine the role
		if($role == GroupMemberPeer::VIEWER) {
			return true;
		} else {
			return false;
		}
	}			
	
	public function isCurrentUser($user_id) {
		if($this->getId() == $user_id) {
			return true;
		} else {
			return false;
		}
	}
	
	protected function getGroupRole($group_id) {
		$member = GroupMemberPeer::retrieveByUserIdAndGroupId($this->getId(), $group_id);
		if($member) {
			return $member->getRole();
		} else {
			return GroupMemberPeer::NONE;
		}
	}
	
	function isFacebookUser() {
		return $this->getAttribute('facebook_id', false);
	}
	
	public function isLoggedInFacebookUser() {
		if (!$this->isFacebookUser())
			return false;
		
		try {
			FacebookUtility::get_info(709259, 'first_name');
		} catch (FacebookRestClientException $ex) {
			return false;
		}
		return true;
	}
	
	public function logout() {
		$this->clearCredentials();
		$this->setAuthenticated(false);
		$this->getAttributeHolder()->clear();
		$this->setFlash('success',"");
		$this->setFlash('error',"");
		$this->setFlash('warning',"");
		$this->setFlash('notice',"");
	}
}
